package cn.zyq.ykb.service.config.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.config.ConfigNotice;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.mapper.config.ConfigNoticeMapper;
import cn.zyq.ykb.service.config.ConfigNoticeService;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.common.PageParam;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 配置-公告提醒Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-12-04
 */
@Service
public class ConfigNoticeServiceImpl extends BaseService implements ConfigNoticeService {

    @Autowired
    private ConfigNoticeMapper configNoticeMapper;

    /**
     * 查询配置-公告提醒
     *
     * @param configNoticeID 配置-公告提醒主键
     * @return 配置-公告提醒
     */
    @Override
    public ConfigNotice selectConfigNoticeByConfigNoticeID(Integer configNoticeID) {
        ConfigNotice configNotice = configNoticeMapper.selectByPrimaryKey(configNoticeID);
        if (configNotice.getDeleted() == 1) {
            return null;
        }
        return configNotice;
    }

    /**
     * 查询配置-公告提醒
     *
     * @param serviceID 业务ID
     * @return 配置-公告提醒
     */
    @Override
    public ConfigNotice selectConfigNoticeByServiceID(String serviceID) {
        ConfigNotice param = new ConfigNotice();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.configNoticeMapper.selectOne(param);
    }

    /**
     * 查询配置-公告提醒列表
     *
     * @param pageParam
     * @return 配置-公告提醒
     */
    @Override
    public PageInfo<ConfigNotice> selectConfigNoticeList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<ConfigNotice> list = this.configNoticeMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询配置-公告提醒列表
     *
     * @param pageParam
     * @return 配置-公告提醒
     */
    @Override
    public List<ConfigNotice> selectConfigNoticeAllList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        return this.configNoticeMapper.list(pageParam.getKv());
    }
    /**
     * 新增配置-公告提醒
     *
     * @param param 配置-公告提醒
     * @param user
     * @return 结果
     */
    @Override
    public int insertConfigNotice(ConfigNotice param, User user) {
        ConfigNotice notice = new ConfigNotice();
        notice.setDeleted(0);
        notice.setUserID(user.getUserID());
        ConfigNotice configNotice = this.configNoticeMapper.selectOne(notice);
        if (BeanUtil.isEmpty(configNotice)){
            param.setUserID(user.getUserID());
            this.setCreateBaseData(param, user);
            return this.configNoticeMapper.insertSelective(param);
        }else {
            param.setConfigNoticeID(configNotice.getConfigNoticeID());
            this.setUpdateBaseData(param,user);
            return this.configNoticeMapper.updateByPrimaryKeySelective(configNotice);
        }
    }

    /**
     * 修改配置-公告提醒
     *
     * @param param 配置-公告提醒
     * @return 结果
     */
    @Override
    public int updateConfigNotice(ConfigNotice param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.configNoticeMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除配置-公告提醒
     *
     * @param configNoticeIDs 需要删除的配置-公告提醒主键
     * @return 结果
     */
    @Override
    public int deleteConfigNoticeByConfigNoticeIDs(Integer[] configNoticeIDs) {
        int i = 0;
        for (Integer configNoticeID : configNoticeIDs) {
            i += this.deleteConfigNoticeByConfigNoticeID(configNoticeID);
        }
        return i;
    }

    /**
     * 删除配置-公告提醒信息
     *
     * @param configNoticeID 配置-公告提醒主键
     * @return 结果
     */
    @Override
    public int deleteConfigNoticeByConfigNoticeID(Integer configNoticeID) {
        ConfigNotice configNotice = this.selectConfigNoticeByConfigNoticeID(configNoticeID);
        if (configNotice == null) {
            return 0;
        } else {
            ConfigNotice storeInfo = new ConfigNotice();
            storeInfo.setConfigNoticeID(configNoticeID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.configNoticeMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
